package com.yifi.interceptor;

import java.util.Enumeration;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;

import com.jfinal.aop.Inject;
import com.jfinal.aop.Interceptor;
import com.jfinal.aop.Invocation;
import com.jfinal.core.Controller;
import com.jfinal.core.JFinal;
import com.jfinal.plugin.activerecord.Db;
import com.yifi.common.DBHelper;
import com.yifi.common.SysUtil;
import com.yifi.common.base.BaseController;
import com.yifi.common.constant.ConstantInit;
import com.yifi.common.model.Syslog;
import com.yifi.system.domain.MisUser;
import com.yifi.system.service.MisPostUserService;
import com.yifi.system.service.MisUserService;
import com.yifi.system.service.OnLineServcie;

import cn.hutool.core.util.StrUtil;

/**
 */
public class LoginInterceptor implements Interceptor {
	
	private static final Logger log = Logger.getLogger(LoginInterceptor.class);  
	
	@Inject
	private OnLineServcie onlineservcie;
	
	@Inject
	private MisUserService misUserService;
	@Inject
	private MisPostUserService misPostUserService;

	public void setSession(MisUser mUser,Controller controller)
	{
		HttpServletRequest request = controller.getRequest();
		request.getSession().setMaxInactiveInterval(24*60*60);//以秒为单位
		
		controller.setSessionAttr(ConstantInit.loginUser,mUser);

		controller.setSessionAttr("Gs_uid", mUser.getCcode());//用户登录名
		controller.setSessionAttr("Gs_uname", mUser.getCname()+"("+mUser.getCname()+")");//用户汉字名+登录名，用于数据记录
		controller.setSessionAttr("Gs_user", mUser.getCcode());//用户代码
		controller.setSessionAttr("Gs_name", mUser.getCname());//用户汉字名
		controller.setSessionAttr("Gs_org", mUser.getCworkDept());//用户部门
		controller.setSessionAttr("Gs_bm", mUser.getCworkDept());
		controller.setSessionAttr("Gs_bz", mUser.getCdept());//用户班组
		controller.setSessionAttr("Gs_pass", mUser.getCpass());
		controller.setSessionAttr("Gs_pro", mUser.getC_pro());//用户产权
		String orgname=Db.queryStr("select cname from mis_org where ccode=?",mUser.getCdept());
		controller.setSessionAttr("Gs_orgname",orgname);
	}
	@Override
	public void intercept(Invocation inv) {
		// TODO Auto-generated method stub
		Controller controller = inv.getController();

		HttpSession session = controller.getSession();
		//处理手机APP访问网页重新生成新session
		String platform=controller.getRequest().getParameter("platform");
		if(StrUtil.isNotEmpty(platform))
		{
			if(platform.equals("app"))
			{
				session.invalidate();
			}
			session = controller.getSession();
			String ccode=controller.getRequest().getParameter("ccode");
			if(platform.equals("app")&&session.getAttribute(ConstantInit.loginUser)==null)
			{
				MisUser misUser=new MisUser();
				misUser.setCcode(ccode);
				List<MisUser> list=(List<MisUser>) misUserService.selectList(misUser);
				if(list.size() >0)
				{
					MisUser mUser=list.get(0);
					setSession(mUser,controller);
				}
			}
		}
		
		//集成于 BaseController 类的 controller 才可以 执行下边的 日志记录操作
		if(BaseController.class.isAssignableFrom(controller.getClass()))
		{
			BaseController Bcontroller = (BaseController)controller;
			Syslog reqSysLog = Bcontroller.getAttr("reqSysLog");
			Bcontroller.setReqSysLog(reqSysLog);
			reqSysLog.setSyslog("1"); // 指定日志是否入库

			String actionKey = inv.getActionKey();
			if (!actionKey.startsWith("/login")&&!actionKey.equals("/")
					&&!actionKey.startsWith("/index")&&!actionKey.contains("swagger")
					&&!actionKey.equals("/getQrCode")&&!actionKey.startsWith("/api")
					&&!actionKey.startsWith("/Api")) {
				try {
					MisUser misUser=(MisUser) session.getAttribute(ConstantInit.loginUser);
					if(misUser!=null)
					{
						//根据当前sessionID去判断用户的可用性
						String sessionid=onlineservcie.getSessionId(controller.getRequest());
						
						inv.invoke();
					}
					else
					{
						inv.getController().redirect("/login");
					}
				} catch (Exception e) {
					inv.getController().redirect("/login");
				}
			}
			else
			{
				inv.invoke();
			}
		}
		else
		{
			inv.invoke();
		}
	}
}
